package com.demo.afternoon;

import com.demo.morning.Student;

import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * @ClassName 常用函数式接口
 * @Description TODO
 * @Author tianqinglong
 * @Date 15:27 2022/7/11
 * @Version 1.0
 **/
public class 常用函数式接口 {
    public static void main(String[] args) {
//        Runnable接口();
//        Function接口();
//简化lambda();
        流构造();
    }

    private static void 流构造() {
        Stream stringStream = Stream.of("a", "b", "C");
        stringStream.forEach(System.out::println);

        List<Integer> list = Arrays.asList(100, 21, 32, 22);
        list.stream().forEach(System.out::println);

        Stream<Double> limit = Stream.generate(() -> Math.random()).limit(10);
        limit.forEach(System.out::println);
    }

    private static void 简化lambda() {
//        Consumer<String> con=(str)-> System.out.println(str);
        Consumer<String> con= System.out::println;
        Function<String,Integer> function=(str)->{return Integer.parseInt(str);};
        Function<String,Integer> function1=Integer::parseInt;

        String str="abcdef";
//        Function<Integer,String> function2=(n)->{return  str.substring(n);};
        Function<Integer,String> function2=str::substring;
        System.out.println(function2.apply(2));

//        BiFunction<String,Integer, Student> biFunction=(name,age)->{return new Student(name,age);}
        BiFunction<String,Integer, Student> biFunction=Student::new;
        System.out.println(biFunction.apply("张三",23));
    }

    private static void Function接口() {
      Function<String,Integer> function= str->str.length();
        System.out.println(function.apply("张三"));
        Function<Integer,Integer> function2= i->i+100;
        System.out.println(function2.compose(function).apply("我是张三"));

        Function<Float,Float> function3= f->f*f;
        System.out.println(function3.apply(3.0f));
        Function<Float,Float> function4= f->f*100;
        System.out.println(function4.compose(function3).apply(16.0f));


    }

    private static void Runnable接口() {
        Runnable r= () -> System.out.println("!!!");
        r.run();
    }
}
